﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms.DanhMuc
{
    public partial class FR_ChiNhanh : DevExpress.XtraEditors.XtraForm
    {
        string ma_hoc_van = "";
        string new_id = "";
        bool isUpdate = false;
        IHRM_BRANCHData _IHRM_BRANCHData;
        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_ChiNhanh(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_ChiNhanh(bool _isUpdate, string mahocvan)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            ma_hoc_van = mahocvan;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IHRM_BRANCHData = ( IHRM_BRANCHData)Activator.GetObject(typeof( IHRM_BRANCHData), util.Chuoichinhanh);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();

            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id;
            }
        }

        private void tangid()
        {
            new_id = _icommon.Generate_ID("HRM_BRANCH", "BranchCode", "CN");
        }

        private void loadthongtin()
        {
            HRM_BRANCH hv = Laythongtin(ma_hoc_van);
            txtma.Text = hv.BranchCode;
            txtten.Text = hv.BranchName;
            txtdiachi.Text = hv.Address;
            txtdienthoai.Text = hv.Phone;
            txtfax.Text = hv.Fax;
            txtluongtoithieu.Value= hv.MinimumSalary;
            txtnguoidaidien.Text = hv.PersonName;
            txtslao.Value = hv.Quantity;
            txtslthuc.Value = hv.FactQuantity;
            txtghichu.Text = hv.Description;
        }

        private HRM_BRANCH Laythongtin(string educode)
        {
            DataSet ds = _IHRM_BRANCHData.HRM_BRANCH_Get(educode);

            HRM_BRANCH hv = new HRM_BRANCH();

            if (ds.Tables.Count>0)
            {
                string ma = ds.Tables[0].Rows[0][0].ToString();
                string ten = ds.Tables[0].Rows[0][1].ToString();
                string diachi = ds.Tables[0].Rows[0][2].ToString();
                string dt = ds.Tables[0].Rows[0][3].ToString();
                string fax = ds.Tables[0].Rows[0][4].ToString();
                string luong = ds.Tables[0].Rows[0][5].ToString();
                string daidien = ds.Tables[0].Rows[0][6].ToString();
                string slthuc = ds.Tables[0].Rows[0][8].ToString();
                string slao = ds.Tables[0].Rows[0][7].ToString();
                string ghichu = ds.Tables[0].Rows[0][9].ToString();

                hv.Description = ghichu;
                hv.BranchCode = ma;
                hv.BranchName = ten;
                hv.Address = diachi;
                hv.FactQuantity = int.Parse(slthuc); 
                hv.Fax = fax;
                hv.MinimumSalary = decimal.Parse(luong); 
                hv.PersonName= daidien;
                hv.Phone = dt;
                hv.Quantity = int.Parse(slao);
                
            }

            return hv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string diachi = txtdiachi.Text;
            string phone = txtdienthoai.Text;
            string fax = txtfax.Text;
            string daidien = txtnguoidaidien.Text;
            decimal luong = decimal.Parse(txtluongtoithieu.Text);
            int slthuc = int.Parse(txtslthuc.Text);
            int slao =int.Parse( txtslao.Text);
            string ghichu = txtghichu.Text;
            int kq = _IHRM_BRANCHData.HRM_BRANCH_Insert(ma, ten, diachi, phone, fax, luong, daidien,slao, slthuc, ghichu);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string diachi = txtdiachi.Text;
            string phone = txtdienthoai.Text;
            string fax = txtfax.Text;
            string daidien = txtnguoidaidien.Text;
            decimal luong = decimal.Parse(txtluongtoithieu.Text);
            int slthuc = int.Parse(txtslthuc.Text);
            int slao = int.Parse(txtslao.Text);
            string ghichu = txtghichu.Text;
            int kq = _IHRM_BRANCHData.HRM_BRANCH_Update(ma, ten, diachi, phone, fax, luong, daidien, slao, slthuc, ghichu);
            return kq;
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtdiachi.Text = "";
            txtma.Text = new_id;
            txtten.Text = "";
            txtdienthoai.Text = "";
            txtfax.Text = "";
            txtluongtoithieu.Text = "";
            txtnguoidaidien.Text = "";
            txtslao.Text = "0";
            txtslthuc.Text = "0";
            txtghichu.Text = "";
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}